package cn.imu.appdevexperiment2020.Application.activity.jiemian;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

import cn.imu.appdevexperiment2020.Application.api.ApiClient;
import cn.imu.appdevexperiment2020.Application.bean.user.DataBean;
import cn.imu.appdevexperiment2020.Application.bean.user.UserBean;
import cn.imu.appdevexperiment2020.Application.bean.user.UserListBean;
import cn.imu.appdevexperiment2020.Application.util.AuthUtil;
import cn.imu.appdevexperiment2020.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ContextFragment4 extends Fragment {
    public static final String TAG="Find";
    List<DataBean> data = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_fragment_tab_4, container, false);
        return view;
    }

    @Override
    public void onStop() {
        super.onStop();
        data.clear();
    }

    @Override
    public void onStart() {
        super.onStart();
        init(data);
        final UserAdapter[] adapter = {new UserAdapter(requireActivity(), R.layout.user_item, data)};
        ListView dataListView = requireActivity().findViewById(R.id.user_listview);
        dataListView.setAdapter(adapter[0]);
        Button find1 = requireActivity().findViewById(R.id.list_find1);
        Button find2 = requireActivity().findViewById(R.id.list_find2);
        Button find3 = requireActivity().findViewById(R.id.list_find3);
        find1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et=requireActivity().findViewById(R.id.find_classId);
                RadioGroup rg = requireActivity().findViewById(R.id.find_gender);
                RadioButton rb = requireActivity().findViewById(rg.getCheckedRadioButtonId());
                EditText et2=requireActivity().findViewById(R.id.find_userName);
                EditText et3=requireActivity().findViewById(R.id.find_userId);
                Call<UserListBean> call= ApiClient.getInstance(requireActivity().getApplicationContext()).getReturnData(AuthUtil.GetAuth(),rb.getTag().toString(),Integer.parseInt(et.getText().toString()));
                call.enqueue(new Callback<UserListBean>() {
                    @Override
                    public void onResponse(Call<UserListBean> call, Response<UserListBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(requireActivity().getApplicationContext(),"成功",Toast.LENGTH_SHORT).show();
                                List<DataBean> newDataList=new ArrayList<>();
                                init(newDataList);
                                newDataList.addAll(response.body().getData());
                                adapter[0] =new UserAdapter(requireActivity(), R.layout.user_item,newDataList);
                                dataListView.setAdapter(adapter[0]);
                            }else{
                                Toast.makeText(requireActivity().getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<UserListBean> call, Throwable t) {
                        Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        find2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et2=requireActivity().findViewById(R.id.find_userName);
                Call<UserBean> call= ApiClient.getInstance(requireActivity().getApplicationContext()).getReturnData(AuthUtil.GetAuth(),et2.getText().toString());
                call.enqueue(new Callback<UserBean>() {
                    @Override
                    public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(requireActivity().getApplicationContext(),"成功",Toast.LENGTH_SHORT).show();
                                init(data);
                                DataBean newdata =response.body().getData();
                                data.add(newdata);
                                adapter[0] =new UserAdapter(requireActivity(), R.layout.user_item,data);
                                dataListView.setAdapter(adapter[0]);
                            }else{
                                Toast.makeText(requireActivity().getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<UserBean> call, Throwable t) {
                        Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        find3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText et3=requireActivity().findViewById(R.id.find_userId);
                Call<UserBean> call= ApiClient.getInstance(requireActivity().getApplicationContext()).getReturnData1(AuthUtil.GetAuth(),et3.getText().toString());
                call.enqueue(new Callback<UserBean>() {
                    @Override
                    public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(requireActivity().getApplicationContext(),"成功",Toast.LENGTH_SHORT).show();
                                init(data);
                                DataBean newdata =response.body().getData();
                                data.add(newdata);
                                adapter[0] =new UserAdapter(requireActivity(), R.layout.user_item,data);
                                dataListView.setAdapter(adapter[0]);
                            }else{
                                Toast.makeText(requireActivity().getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<UserBean> call, Throwable t) {
                        Toast.makeText(requireActivity().getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
    private void init(List<DataBean> datalist) {
        datalist.clear();
        DataBean data1=new DataBean();
        data1.setUserName("学号");
        data1.setNickName("姓名");
        data1.setCreateBy("应用");
        data1.setPhonenumber("电话");
        data1.setSex("性别");
        datalist.add(0,data1);
    }
}
